package com.tinem.platform.service.robot.im.core;

import com.tinem.platform.service.robot.im.jpa.entity.RbacUserMapping;
import com.tinem.platform.service.robot.im.jpa.repository.RbacUserMappingRepository;
import com.tinem.platform.service.robot.im.util.UidUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class UserService {

    @Resource
    UidUtil uidUtil;

    @Resource
    RbacUserMappingRepository rbacUserMappingRepository;


    public RbacUserMapping getUserId(String clientId,String clientUserId){
        RbacUserMapping userMapping = rbacUserMappingRepository.findTopByClientIdAndAndClientUserId(clientId,clientUserId);
        if(userMapping == null){
            userMapping = new RbacUserMapping();
            userMapping.setId(uidUtil.getUid());
            userMapping.setClientId(clientId);
            userMapping.setClientUserId(clientUserId);
            userMapping.setCreatedBy(clientUserId);
            Date d = new Date();
            userMapping.setCreatedTime(d);
            userMapping.setUpdatedBy(clientUserId);
            userMapping.setUpdatedTime(d);
            rbacUserMappingRepository.save(userMapping);
        }
        return userMapping;
    }
}
